<?php
require_once(OC_App::getAppPath('resumes') . "/lib/share/share.php");
require_once(OC_App::getAppPath('resumes') . "/lib/resume.php");
require_once(OC_App::getAppPath('resumes') . "/lib/helper.php");
require_once(OC_App::getAppPath('resumes') . "/lib/tag.php");
require_once(OC_App::getAppPath('resumes') . "/controllers/resume.php");

// Load other apps for file previews
OC_App::loadApps();

$token = JRequest::getString('t', '');
$sharewith = JRequest::getString('w', '');
if (!empty($token) && !empty($sharewith)) {
   $items = ResumeShare::getSharesByToken($token, $sharewith);
   if(!$items) {
      header('HTTP/1.0 404 Not Found');
      $tmpl = new OCP\Template('', '404', 'base');
      $tmpl->assign('error', '没有找到相关的分享信息。请联系给您分享该链接的用户，已确保该链接正确。');
      $tmpl->printPage();
   }
   elseif(count($items) <= 1 && $items[0]->share_with == NULL) {
      /* redirect to login */
      if(!OC_User::isLoggedIn()){
         PUtil::checkUserLogin(true,OCP\Util::linkToRoute('resume_view', array('id' => $items[0]->item_source)));
      }else{
         header('HTTP/1.0 404 Not Found');
         $tmpl = new OCP\Template('', '404', 'base');
         $tmpl->assign('error', '没有找到相关的分享信息。请联系给您分享该链接的用户，已确保该链接正确。');
         $tmpl->printPage();
      }
   }elseif(OC_User::isLoggedIn()) {
      // login user, do redirect 
      OC_Response::redirect(OCP\Util::linkToRoute('resume_view', array('id' => $items[0]->item_source)));
   }
   else {
      $masteruser = ResumeShare::getCompanyIdFromOwner($items[0]->uid_owner); 
      if(!$masteruser || $masteruser->id <= 0) {
         // invalid sharing 
      }
      else {
         $model = new PResumesModelResume();
         $resumes = $model->getResumeAsFiles($masteruser->id, array($items[0]->item_source));
         if(empty($resumes)) {
            $tmpl = new OC_Template('', '404', 'user');
            $tmpl->assign('error', '简历未找到');
            $tmpl->printPage();
            return;
         }


         $app = PFactory::getApplication(); 
         $app->setUserState('resumeshare_companyid', $masteruser->id); 
         $resume = $resumes[0];
        
         // update new status 
         if($resume->is_new) PResumeController::setResumeNewStatus($resume->id); 

         // get persontest info
         $resume->pResult = '没有结果';
         $persontestResultInfo = $model->getPersontestResultInfo($masteruser->id, $resume->id, true);
         if (!empty($persontestResultInfo->shortresult)) {
            $resume->pResult = $persontestResultInfo->shortresult;
         }

         // convert date_added to unix timestamp 
         $resume->date_added = PFactory::getDate($resume->date_added)->toUnix(); 
 
         // get resume tags
         $tagmodel = new PResumesModelTag();
         $resumeTags = $tagmodel->getResumeTags($resume->id, $masteruser->id);

         // for non-logged in user, the permission is default to read
         $permissions = OCP\PERMISSION_READ;

         $dir = 'shared'; 
         $prevresume = $nextresume = null; 
         $fromdir = 'shared';

         // we have iframe in this page.
         header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');

         $part_resume_top = new OCP\Template('resumes', 'part.resume.top', '');
         $part_resume_top->assign('fromdir', $fromdir);
         $part_resume_top->assign('breadcrumb', '');

         $tmpl = new OCP\Template('resumes', 'resume', 'user');
         $tmpl->assign('page_title', $resume->name . "-" . $resume->jobtitle);
         $tmpl->assign('permissions', $permissions);
         $tmpl->assign('dir', $dir);
         $tmpl->assign('fromdir', $fromdir);
         $tmpl->assign('part_resume_top', $part_resume_top->fetchPage());

         $tmpl->assign('allowZipDownload', false); 

         $tmpl->assign('resume', $resume);
         $tmpl->assign('resumeTags', $resumeTags);
         $tmpl->assign('prevresume', $prevresume);
         $tmpl->assign('nextresume', $nextresume);
         $tmpl->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
         $tmpl->assign('downloadURL', OCP\Util::linkToRoute('resume_download', array('resume' => '/')));
         $tmpl->assign('viewURL', OCP\Util::linkToRoute('resume_view', array('id' => '')));
         $tmpl->printPage();
      }
   }
}
elseif(!empty($sharewith)) {
   OC_Response::redirect(OC_Helper::linkToRoute('company_login')); 
}
else {
   header('HTTP/1.0 404 Not Found');
   $tmpl = new OCP\Template('', '404', 'base');
   $tmpl->assign('error', '没有找到相关的分享信息。请联系给您分享该链接的用户，已确保该链接正确。');
   $tmpl->printPage();
}

